package wqh.dao.impl;

import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.stereotype.Repository;
import wqh.dao.XkMonitorGeoDao;
import wqh.domain.XkDevice;
import wqh.domain.XkMonitorGeo;

import java.util.List;

/**
 * Created by wqh on 2017/1/13.
 */
@Repository("xkMonitorGeoDao")
public class XkMonitorGeoDaoImpl extends DaoImpl<XkMonitorGeo> implements XkMonitorGeoDao {

    @Override
    public XkMonitorGeo findByDeviceIdAndSensorId(Integer sensor_id, Long device_id) {
        Session session = this.getSessionFactory().getCurrentSession();
        String hql = "FROM XkDevice AS x WHERE x.device_id = :device_id";
        Query query = session.createQuery(hql);
        query.setParameter("device_id",device_id);
        List<XkDevice> list = query.list();
        if(list == null ) return null;
        long xk_id = list.get(0).getId();
        hql = "FROM XkMonitorGeo AS x WHERE xk_id = :xk_id AND sensor_id = :sensor_id";
        query = session.createQuery(hql);
        query.setParameter("xk_id",xk_id);
        query.setParameter("sensor_id",sensor_id);
        return query.list() == null ? null : (XkMonitorGeo) query.list().get(0);
    }

    @Override
    public List<XkMonitorGeo> findByGeoId(Long geo_id) {
        Session session = this.getSessionFactory().getCurrentSession();
        String hql = "FROM XkMonitorGeo AS x WHERE x.geoLocation.id = :geo_location_id";
        Query query = session.createQuery(hql);
        query.setParameter("geo_location_id",geo_id);
        return query.list();
    }
}
